
package com.gzbsoft.sc.modules.sys.service.impl;
import com.gzbsoft.sc.modules.sys.dao.SysMenuDao;
import com.gzbsoft.sc.modules.sys.dao.SysUserDao;
import com.gzbsoft.sc.modules.sys.dao.SysUserTokenDao;
import com.gzbsoft.sc.modules.sys.entity.SysUser;
import com.gzbsoft.sc.modules.sys.entity.SysUserToken;
import com.gzbsoft.sc.modules.sys.service.ShiroService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Service
public class ShiroServiceImpl implements ShiroService {
    private final SysMenuDao sysMenuDao;
    private final SysUserDao sysUserDao;
    private final SysUserTokenDao sysUserTokenDao;
    public ShiroServiceImpl(SysMenuDao sysMenuDao, SysUserDao sysUserDao, SysUserTokenDao sysUserTokenDao) {
        this.sysMenuDao = sysMenuDao;
        this.sysUserDao = sysUserDao;
        this.sysUserTokenDao = sysUserTokenDao;
    }
    @Override
    public Set<String> getUserPermissions(Long userId) {
        List<String> permsList = sysUserDao.db().createSqlQuery("select perms from sys_menu menu, ref_sys_user_role user_role, ref_sys_role_menu role_menu where user_role.sys_role_id = role_menu.role_id " +
                " and role_menu.menu_id = menu.id and user_role.sys_user_id = "+userId).findSingleAttributeList(String.class);
        //用户权限列表
        Set<String> permsSet = new HashSet<>();
        for (String perms : permsList) {
            permsSet.addAll(Arrays.asList(perms.trim().split(",")));
        }
        return permsSet;
    }
    @Override
    public SysUserToken queryByToken(String token) {
        return sysUserTokenDao.queryByToken(token);
    }
    @Override
    public SysUser queryUser(Long userId) {
        return sysUserDao.findById(userId).orElse(null);
    }
}
